//
//  ApplyViewController.m
//  YXFinance
//
//  Created by uplooking on 2018/10/25.
//  Copyright © 2018年 CRLong. All rights reserved.
//

#import "ApplyOldViewController.h"
@interface ApplyOldViewController ()
<
UITextFieldDelegate
>
{
    UIScrollView * scrollView;
    UITextField * nameTextField;
    UITextField * identityCardTextField;
    UITextField * nickTextField;
    UITextField * phoneTextField;
    UITextField * codeTextField;
    UITextField * teacherTextField;
    
    UILabel * labelCode;
    NSTimer * timer;
    NSInteger count;
    UIButton * codeBtn;
    NSString * teacher_id;
}

@end

@implementation ApplyOldViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"立即报名";
    [self setBackLeftBarButtonItemAction:@selector(backAction:)];
    [self initView];
    
}

- (void)backAction:(UIButton *)sender{
    
    [self.navigationController popToRootViewControllerAnimated:YES];
    
}


- (void)initView{
    
    teacher_id = @"";
    
    scrollView=[[UIScrollView alloc]init];
    [self.view addSubview:scrollView];
    [scrollView setShowsHorizontalScrollIndicator:NO];
    [scrollView setShowsVerticalScrollIndicator:NO];
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    
    UIView *vContainer = [UIView new];
    [scrollView addSubview:vContainer];
    [vContainer mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(scrollView);
        make.width.equalTo(scrollView);
    }];
    
    
    NSArray * array = @[@"选手姓名",
                        @"身份证号",
                        @"选手昵称",
                        @"联系电话",
                        @"验  证  码",
                        @"导        师"];
    
    
    NSArray * pArray = @[@"选手姓名与选手昵称不可相同",
                         @"请填写身份证号",
                         @"请输入选手昵称",
                         @"请输入联系电话",
                         @"请输入验证码",
                         @"请选择导师"];
    
    UIView * lastView = nil;
    NSInteger count = array.count;
    
    for (NSInteger i = 0; i < count; i++) {
        
        UIView * item = [[UIView alloc]init];
        [item setBackgroundColor:[UIColor whiteColor]];
        [vContainer addSubview:item];
        [item mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(lastView == nil ? vContainer :lastView.mas_bottom);
            make.left.right.equalTo(vContainer);
            make.height.mas_equalTo(55 * kAutoScanleX);
        }];
        
        UILabel * label = [[UILabel alloc]init];
        [label setTextColor:HexRGBA(0x585858, 1)];
        [label setText:array[i]];
        [label setFont:[UIFont systemFontOfSize:14 * kAutoScanleX]];
        [label setTextAlignment:NSTextAlignmentRight];
        [item addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(item);
            make.left.equalTo(item).offset(15 * kAutoScanleX);
            make.width.mas_equalTo(60 * kAutoScanleX);
            make.height.mas_equalTo(25 * kAutoScanleX);
        }];
        
        
        [self setBomLine:item color:HexRGBA(0xd5d9db, 1)];
        
        
        UITextField * textField =[[UITextField alloc]init];
        [textField setDelegate:self];
        [item addSubview:textField];
        [textField setTextColor:HexRGBA(0x000000, 1)];
        [textField setKeyboardType:UIKeyboardTypeEmailAddress];
        [textField setReturnKeyType:UIReturnKeyDone];
        [textField setFont:[UIFont systemFontOfSize:14]];
        [textField setValue:HexRGBA(0xa7a7a7, 1) forKeyPath:@"_placeholderLabel.textColor"];
        [textField setPlaceholder:pArray[i]];
        [textField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(item);
            make.left.equalTo(label.mas_right).offset(30);
            make.right.equalTo(item).offset(-15);
            make.height.mas_equalTo(30);
        }];
        
        if (i == 0) {
            
            nameTextField = textField;
        }
        
        if (i == 1) {
            
            identityCardTextField = textField;
            
        }
        
        if (i == 2) {
            
            nickTextField = textField;
        }
        
        if (i == 3) {
            
            phoneTextField = textField;
            [item mas_updateConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(lastView.mas_bottom).offset(10 * kAutoScanleX);
            }];
        }
        
        if (i == 4) {
            
            codeTextField = textField;
            [textField mas_updateConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(item).offset(-100 * kAutoScanleX);
            }];
            
            labelCode =[[UILabel alloc]init];
            [item addSubview:labelCode];
            [labelCode setText:@"获取验证码"];
            [labelCode setBackgroundColor:HexRGBA(0xa4a4a4, 1)];
            [labelCode setFont:[UIFont systemFontOfSize:11 * kAutoScanleX]];
            [labelCode setTextColor:HexRGBA(0xffffff, 1)];
            [labelCode setTextAlignment:NSTextAlignmentCenter];
            [labelCode.layer setMasksToBounds:YES];
            [labelCode.layer setCornerRadius:4 * kAutoScanleX];
            [labelCode mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(item);
                make.right.equalTo(item).offset(-15 * kAutoScanleX);
                make.size.mas_equalTo(CGSizeMake(70 * kAutoScanleX, 24 * kAutoScanleX));
            }];
            
            
            codeBtn = [[UIButton alloc]init];
            [codeBtn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
            [item addSubview:codeBtn];
            [codeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.edges.equalTo(labelCode);
            }];
            
            
            UILabel * labelTitle = [[UILabel alloc]init];
            [labelTitle setText:@"该电话需与券商开户手机号保持一致"];
            [labelTitle setTextColor:HexRGBA(0xb99c6d, 1)];
            [labelTitle setFont:[UIFont systemFontOfSize:12 * kAutoScanleX]];
            [labelTitle setTextAlignment:NSTextAlignmentCenter];
            [vContainer addSubview:labelTitle];
            [labelTitle mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.equalTo(vContainer);
                make.top.equalTo(item.mas_bottom);
                make.height.mas_equalTo(40 * kAutoScanleX);
            }];
            
        }
        
        if (i == 5) {
            
            teacherTextField = textField;
            
            [self setBackRightArrow:item];
            
            [textField setTextAlignment:NSTextAlignmentRight];
            
            [item mas_updateConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(lastView.mas_bottom).offset(40 * kAutoScanleX);
            }];
            
            [textField mas_updateConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(item).mas_offset(-45 * kAutoScanleX);
            }];
            
            UIButton * button = [[UIButton alloc]init];
            [button addTarget:self action:@selector(buttonTeacherAction:) forControlEvents:UIControlEventTouchUpInside];
            [item addSubview:button];
            [button mas_makeConstraints:^(MASConstraintMaker *make) {
                make.edges.equalTo(item);
            }];
            
        }
        
        lastView = item;
    }
    
    
    UIButton * nextBtn=[[UIButton alloc]init];
    [nextBtn setBackgroundColor:HexRGBA(0xb99c6d, 1)];
    [nextBtn addTarget:self action:@selector(nextBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [nextBtn.layer setMasksToBounds:YES];
    [nextBtn.layer setCornerRadius:8.0 * kAutoScanleX];
    [nextBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [nextBtn setTitle:@"下一步" forState:UIControlStateNormal];
    [nextBtn.titleLabel setFont:[UIFont systemFontOfSize:16 * kAutoScanleX]];
    [vContainer addSubview:nextBtn];
    [nextBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lastView.mas_bottom).offset(20 * kAutoScanleX);
        make.height.mas_equalTo(45 * kAutoScanleX);
        make.centerX.equalTo(vContainer);
        make.width.mas_equalTo(576/2.0 * kAutoScanleX);
    }];
    
    [vContainer mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(nextBtn).offset(30 * kAutoScanleX);
    }];
    
}


#pragma mark -- 选择导师
- (void)buttonTeacherAction:(UIButton *)sender{
    
    TeacherListViewController * vc = [[TeacherListViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
    [vc setBlockChooseFinish:^(TeacherIndexModel * _Nonnull tModel) {
        teacher_id = tModel.t_id;
        [teacherTextField setText:tModel.teacher_name];
    }];
    
}

#pragma mark -- 下一步

- (void)nextBtnAction:(UIButton *)sender{
    //
    WS(weakSelf);
    
    NSString * mobile = phoneTextField.text;
    NSString * full_name = nameTextField.text;
    NSString * idcard = identityCardTextField.text;
    NSString * nickname = nickTextField.text;
    NSString * vcode = codeTextField.text;
    
    
    if (mobile.length != 11) {
        
        return[self showHint:@"请填写正确的手机号"];
    }
    
    if (full_name.length == 0) {
        
        return[self showHint:@"请填写真实姓名"];
    }
    
    
    
    if (idcard.length == 0) {
        
        return[self showHint:@"请填写身份证号"];
    }
    
    
    if (nickname.length == 0) {
        
        return[self showHint:@"请填写您的昵称"];
    }
    
    
    if (vcode.length == 0) {
        
        return[self showHint:@"请填写验证码"];
    }
    
    if (teacher_id.length == 0) {
        
        return[self showHint:@"请选择导师"];
    }
    
    [self showHudInView:self.view hint:@""];
    [HttpRequestHelper requestApiRegisterSignupMobile:mobile full_name:full_name idcard:idcard nickname:nickname vcode:vcode teacher_id:teacher_id Block:^(NSString *msg, NSString *html5_jump_url) {
        [weakSelf hideHud];
        if (REQUEST_SUCCESS) {
            
            [weakSelf sumbitFinish:html5_jump_url];
            [[NSNotificationCenter defaultCenter] postNotificationName:REFRESH_USER_INFO object:nil];
            
        }else{
            
            [weakSelf showHint:msg];
        }
    }];
    
    
}

#pragma mark -- 提交完成
- (void)sumbitFinish:(NSString *)url{
    
    
    PSTAlertController *vc = [PSTAlertController alertControllerWithTitle:@"恭喜您，报名成功!" message:nil preferredStyle:PSTAlertControllerStyleAlert];
    
    [vc addAction:[PSTAlertAction actionWithTitle:@"知道了" handler:^(PSTAlertAction * _Nonnull action) {
        
        MyWebViewController * vc = [[MyWebViewController alloc]init];
        vc.url = url;
        [self.navigationController pushViewController:vc animated:YES];
        
    }]];
    
    
    [vc showWithSender:nil controller:self animated:YES completion:nil];
    
    
}



#pragma mark--获取验证码
-(void)buttonAction:(UIButton *)sender{
    
    [ApplicationDelegate.window endEditing:YES];
    
    if (phoneTextField.text.length!=11) return [self showHint:@"请核对您的手机号"];
    
    WS(weakSelf);
    
    [weakSelf setAuthCodeSuccess];
    
    [HttpRequestHelper requestApiAuthCodeMobile:phoneTextField.text check:0 Block:^(NSString *code, NSString *msg) {
        
        if (REQUEST_SUCCESS) {
            
            NSLog(@"code : %@",code);
            
        }else{
            
            [weakSelf showHint:msg];
        }
    }];
    
}

-(void)setAuthCodeSuccess{
    
    count = 60;
    
    timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(reduceTime:) userInfo:nil repeats:YES];
    
    //    [self showHint:@"验证码已经发送到您的手机"];
    
}

#pragma mark--定时器
-(void)reduceTime:(NSTimer *)_timer{
    
    count--;
    
    count == 0 ? [self labelNormal] : [self labelSelected];
    
}
-(void)labelNormal{
    
    [labelCode setText:@"获取验证码"];
    [codeBtn setUserInteractionEnabled:YES];
    [timer invalidate];
    
}
-(void)labelSelected{
    
    
    [labelCode setText:[NSString stringWithFormat:@"%ld s",count]];
    [codeBtn setUserInteractionEnabled:NO];
    
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    [self.view endEditing:YES];
    
    return YES;
}


#pragma mark--弹起键盘
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    
    
}
-(void)viewWillDisappear:(BOOL)animated{
    
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
    
}

#pragma mark--弹起键盘
- (void)keyboardWillShow:(NSNotification *)notification
{
    NSDictionary *userInfo = notification.userInfo;
    
    CGRect keyboardF = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGFloat height = keyboardF.size.height;
    
    
    [UIView animateWithDuration:0.25 animations:^{
        
        [scrollView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.view).offset(-height);
        }];
        
        [self.view layoutIfNeeded];
        
    }];
    
    
}

- (void)keyboardWillHide:(NSNotification *)notification
{
    
    [UIView animateWithDuration:0.25 animations:^{
        
        [scrollView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.view);
        }];
        
        [self.view layoutIfNeeded];
        
    }];
    
    
}

@end

